<script setup>
	import {
		onShow,
		onLoad,
		onLaunch,
		onHide
	} from "@dcloudio/uni-app"
	import request from './api/index.js';
	import {
		getColorsList
	} from '@/common/common.js'


	onLaunch(() => {




	})
	onShow(() => {
		getUrlParameter()
		getColorsList();
		// window.alert("onShow－－－－－－－－－－－－－" + window.location.search)
		console.log('App Show')

	})
	


	onHide(() => {
		console.log('App Hide')
	})

	const getUrlParameter = () => {
		
		
		
		console.log("uniq")
		if (window.location.search) {
			const urlParams = new URLSearchParams(window.location.search);
			let code = urlParams.get('code');
			// window.alert("code----App.vue----"+ code); // 输出 'sXtElRsqUy2ZyM7IZ01T4IVbFNW3Bj4DRr5nzRF5eQ8'
			if (code) {
				getWXUserInfo(code)
			}


		}
	}

	const getWXUserInfo = (code) => {

		request({
			url: '/api/wx/work/core/loginByCode',
			method: 'GET',
			data: {
				code: code,
			}
		}, e => {
			if (e.success) {
				// window.alert("e.data.token----App.vue----" + JSON.stringify(e.data.token));
				uni.setStorage({
					key: 'token',
					data: {
						token: e.data.token
					},
					success() {
						getDictData()
						getUserCurrent()

					}
				})
			}else{
				// window.alert("e.error----" + JSON.stringify(e));
				
			}
		})
	}

	const getUserCurrent = () => {
		request({
			url: '/api/sys/user/current',
			method: 'GET',

		}, e => {
			if (e.success) {

				uni.setStorage({
					key: 'userData',
					data: e.data

				})


			}
		})
	}


	const getDictData = () => {
		request({
			url: '/api/sys/dict/items',
			method: 'GET',
		}, e => {

			if (e.success) {
				console.log("dict-----------", e.data)
				const groupedDictData = e.data.reduce((acc, item) => {
					const dictCode = item.dictCode;
					if (!acc[dictCode]) {
						acc[dictCode] = [];
					}
					acc[dictCode].push(item);
					return acc;
				}, {});

				console.log("groupedDictData--------", groupedDictData);
				uni.setStorage({
					key: 'dicts',
					data: groupedDictData

				})

			}
		})
	}
</script>

<style>
	@import url('static/style/global.css');

	/*每个页面公共css */
	uni.page-head {
		display: none;
	}
</style>